﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSAcademic.v2.Database;

namespace MSAcademic.v2.Infrastructure.Tag.Impl
{
    public class TagQueriesImpl : ITagQueries
    {
      public ITag Get(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbtag = db.Tags
                    .Where(x => x.Id == id).SingleOrDefault();

                var tag = new TagImpl
                {
                    Id = dbtag.Id,
                    Name = dbtag.Name
                };

                return tag;
            }
          
        }

      public ITag TryGetByName(string name)
      {
          using (var db = new MSAcademicDatabaseContainer())
          {
              var dbtag = db.Tags
                  .Where(x => x.Name == name).SingleOrDefault();

              var tag = new TagImpl
              {
                  Id = dbtag.Id,
                  Name = dbtag.Name
              };

              return tag;
 
          }
      }

      public IEnumerable<ITag> GetAll()
      {
          using (var db = new MSAcademicDatabaseContainer())
          {
              var dbTagList = db.Tags
                  .Where(x => x.Id != null)
                  .OrderByDescending(x => x.Name)
                  .Select(x => new TagImpl
                  {
                      Id = x.Id,
                      Name = x.Name
                  }).ToList();

              return dbTagList;

          }
      }

      public IEnumerable<ITag> GetSelectedTags(List<int> selectedTags)
      {
          using(var db= new MSAcademicDatabaseContainer())
          {
              if (selectedTags == null) return null;
              var tags= db.Tags.Where(m => selectedTags.Contains(m.Id)).Select(m => new TagImpl
                                                                                         {
                                                                                             Id = m.Id,
                                                                                             Name = m.Name
                                                                                         }).ToList();
              return tags;
          }
      }

      public IEnumerable<ITag> GetTagsForArticle(int articleId)
      {
          using(var db= new MSAcademicDatabaseContainer())
          {
              var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
              if(article==null) return null;
              var articleTags = article.Tags.Select(m => new TagImpl
                                                             {
                                                                 Id = m.Id,
                                                                 Name = m.Name
                                                             }).ToList();
              return articleTags;

          }
      }

    }
}
